package com.sumer.psychology.photo.resource;

import com.sumer.psychology.common.application.ApplicationServiceRegistry;
import com.sumer.psychology.common.resource.AbstractResource;
import com.sumer.psychology.photo.application.command.*;
import com.sumer.psychology.photo.domain.model.AlbumId;
import com.sumer.psychology.photo.domain.model.PhotoId;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 照片rest接口
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/29
 * Time: 8:39
 */
@Path("/v1/photo/")
@Consumes(MediaType.APPLICATION_JSON)
public class PhotoResource extends AbstractResource {
    @Context
    private UriInfo uriInfo;

    /**
     * 新建相册
     *
     * @param aCreateAlbumCommand command
     * @return resposne
     */
    @POST
    @Path("album")
    public Response addAlbum(CreateAlbumCommand aCreateAlbumCommand) {
        AlbumId albumId = ApplicationServiceRegistry
                .albumApplicationService()
                .newAlbum(aCreateAlbumCommand);

        return Response
                .status(Response.Status.CREATED)
                .location(uriInfo.getAbsolutePath())
                .entity(Collections.singletonMap("_id", albumId.identity()))
                .build();
    }

    /**
     * 编辑相册
     *
     * @param albumId             id
     * @param aChangeAlbumCommand command
     * @return response
     */
    @PUT
    @Path("album/{albumId}")
    public Response editAlbum(
            @PathParam("albumId") String albumId,
            ChangeAlbumCommand aChangeAlbumCommand) {

        ApplicationServiceRegistry
                .albumApplicationService()
                .change(aChangeAlbumCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 删除相册
     *
     * @param albumId id
     * @return response
     */
    @DELETE
    @Path("album/{albumId}")
    public Response deleteAlbum(@PathParam("albumId") String albumId) {
        ApplicationServiceRegistry
                .albumApplicationService()
                .delete(new DeleteAlbumCommand(albumId));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 批量增加照片
     *
     * @param uploadPhotoCommands commands
     * @return response
     */
    @POST
    public Response addPhoto(UploadPhotoCommand[] uploadPhotoCommands) {
        List<String> photoIds = new ArrayList<>(uploadPhotoCommands.length);
        for (UploadPhotoCommand aUploadPhotoCommand : uploadPhotoCommands) {
            PhotoId photoId = ApplicationServiceRegistry
                    .photoApplicationService()
                    .upload(aUploadPhotoCommand);
            photoIds.add(photoId.identity());
        }

        return Response
                .status(Response.Status.CREATED)
                .location(uriInfo.getAbsolutePath())
                .entity(photoIds)
                .build();
    }

    /**
     * 编辑照片
     *
     * @param photoId             photo id
     * @param aChangePhotoCommand command
     * @return response
     */
    @PUT
    @Path("{photoId}")
    public Response editPhoto(
            @PathParam("photoId") String photoId,
            ChangePhotoCommand aChangePhotoCommand) {

        ApplicationServiceRegistry
                .photoApplicationService()
                .change(aChangePhotoCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 删除照片
     *
     * @param photoId photo id
     * @return response
     */
    @DELETE
    @Path("{photoId}")
    public Response deletePhoto(@PathParam("photoId") String photoId) {
        ApplicationServiceRegistry
                .photoApplicationService()
                .delete(new DeletePhotoCommand(photoId));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 查询一个人拥有的所有相册
     *
     * @param ownerId      id
     * @param countPerPage count per page
     * @param offset       offset
     * @return respnose
     */
    @GET
    @Path("album/owner/{ownerId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findAlbumByOwner(
            @PathParam("ownerId") String ownerId,
            @QueryParam("countPerPage") int countPerPage,
            @QueryParam("offset") int offset) {
        String results = ApplicationServiceRegistry
                .albumQueryApplicationService()
                .findByAuthor(ownerId, countPerPage, offset);

        return response(results);
    }

    /**
     * 查询一个相册下的所有照片
     *
     * @param albumId      id
     * @param countPerPage count per page
     * @param offset       offset
     * @return response
     */
    @GET
    @Path("album/{albumId}/photos")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findPhotosOfAlbum(
            @PathParam("albumId") String albumId,
            @QueryParam("countPerPage") int countPerPage,
            @QueryParam("offset") int offset) {
        String results = ApplicationServiceRegistry
                .photoQueryApplicationService()
                .listByAlbum(albumId, countPerPage, offset);

        return response(results);
    }

    /**
     * 查询一个照片
     *
     * @param photoId id
     * @return response
     */
    @GET
    @Path("{photoId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findOnePhoto(@PathParam("photoId") String photoId) {
        String results = ApplicationServiceRegistry
                .photoQueryApplicationService()
                .findById(photoId);

        return response(results);
    }

    /**
     * 查询热门照片
     *
     * @param countPerPage count per page
     * @param offset       offset
     * @return response
     */
    @GET
    @Path("topics")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findTopicPhotos(
            @QueryParam("countPerPage") int countPerPage,
            @QueryParam("offset") int offset) {
        String results = ApplicationServiceRegistry
                .photoQueryApplicationService()
                .findTopic(countPerPage, offset);

        return response(results);
    }

    /**
     * 查询一个相册
     *
     * @param albumId id
     * @return response
     */
    @GET
    @Path("album/{albumId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findOneAlbum(@PathParam("albumId") String albumId) {
        String results = ApplicationServiceRegistry
                .albumQueryApplicationService()
                .findById(albumId);

        return response(results);
    }

}
